<?php namespace App\Repositories\Eloquent\User;

use App\Models\Publisher;
use App\Models\PublisherInviteCode;
use App\Repositories\User\PublisherRepositoryInterface;

class PublisherRepository implements PublisherRepositoryInterface
{
    /**
     * @param $id
     *
     * @return \App\Models\Publisher
     */
    public function show($id)
    {
        return Publisher::query()->find((int)$id);
    }

    /**
     * 是否有效发布者
     *
     * @param $id
     *
     * @return bool
     */
    public function isValidPublisher($id)
    {
        $publisher = $this->show($id);

        return $publisher && $publisher->isValid();
    }

    /**
     * 创建发布者
     *
     * @param $id
     */
    public function createPublisher($id)
    {
        $publisher = Publisher::firstOrNew(['user_id' => $id]);
        $publisher->status = Publisher::VALID;
        $publisher->save();
    }

    /**
     * 通过邀请码获取记录
     *
     * @param $code
     *
     * @return \App\Models\PublisherInviteCode||null
     */
    public function findCoderByCode($code)
    {
        return PublisherInviteCode::query()->where('code', $code)->first();
    }

    /**
     * 受邀发布者绑定邀请码
     *
     * @param $coder
     * @param $invitee_uid
     *
     * @return bool
     */
    public function inviteeBindCoder($coder, $invitee_uid)
    {
        if (!($coder instanceof PublisherInviteCode)) {
            $coder = $this->findCoderByCode((string)$coder);
        }

        $coder->invitee_uid = $invitee_uid;

        return $coder->save();
    }

    /**
     * 统计发布者的邀请者数量
     *
     * @param $publisher_uid
     *
     * @return int
     */
    public function publisherInviteeCount($publisher_uid)
    {
        return PublisherInviteCode::query()
            ->where('publisher_uid', $publisher_uid)
            ->where('invitee_uid', '>', 0)
            ->count();
    }

    /**
     * 统计发布者的邀请码数量
     *
     * @param $publisher_uid
     *
     * @return int
     */
    public function publisherInviteCodeCount($publisher_uid)
    {
        return PublisherInviteCode::query()
            ->where('publisher_uid', $publisher_uid)
            ->count();
    }

    /**
     * 创建邀请码
     *
     * @param $publisher_uid
     *
     * @return \App\Models\PublisherInviteCode
     */
    public function createInviteCoder($publisher_uid)
    {
        $coder = new PublisherInviteCode();
        $coder->publisher_uid = $publisher_uid;
        $coder->code = str_random(8);
        $coder->save();

        return $coder;
    }

    /**
     * 获取卖家邀请人
     *
     * @param $publisher_uid
     *
     * @return mixed|null
     */
    public function getPublisherInviteUid($publisher_uid)
    {
        $invitee = $this->getPublisherInvite($publisher_uid);

        return $invitee ? $invitee->publisher_uid : null;
    }

    /**
     * 获取卖家邀请记录
     *
     * @param $invitee_uid
     *
     * @return \Illuminate\Database\Eloquent\Model|null|static
     */
    public function getPublisherInvite($publisher_uid)
    {
        return PublisherInviteCode::query()
            ->where('publisher_uid', $publisher_uid)
            ->where('invitee_uid', '>', 0)
            ->first();
    }

}
